package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

import com.gusha.service.InfoContentService;
import com.gusha.dao.InfoContentDao;
import com.gusha.dao.SysClassifyDao;
import com.gusha.model.InfoContent;
import com.gusha.model.SysClassify;


/**
 * @Created：2018-10-30 14:25:23
 * @Author LightSound的代码生成器
 * @Description:InfoContentController控制类
 */
@CrossOrigin(origins="*",maxAge=3600)
@Controller
@RequestMapping("/gx/infoContent")
public class InfoContentController {

    // 注入Dao
    @Autowired
    private InfoContentDao infoContentDao;
    @Autowired
    private SysClassifyDao sysClassifyDao;
    
    // 注入Service
    @Autowired
    private InfoContentService infoContentService;

    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看资讯列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String infoTitle = WebValueUtil.getWebValue(request, "infoTitle");
        String classifyId = WebValueUtil.getWebValue(request, "classifyId");

        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        InfoContent infoContent = new InfoContent();
        if (StringUtils.isNotBlank(infoTitle)) {
        	infoContent.setInfoTitle(infoTitle);
        }
        if (StringUtils.isNotBlank(classifyId)) {
        	infoContent.setClassifyId(new BigInteger(classifyId));
        }else {
        	classifyId = "0";
        }

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<InfoContent> page = infoContentService.findAll(pageable, infoContent);

        List<SysClassify> listParent = sysClassifyDao.findAllByParentIdAndIsDelete(new BigInteger(Constants.CLASSIFY_INFO),0);
        
        model.addAttribute("page", page);
        model.addAttribute("listParent", listParent);
        model.addAttribute("classifyId", new BigInteger(classifyId));
        model.addAttribute("infoTitle", infoTitle);
        return "/admin/infoContent/list";
    }
    
   
    // 进入添加页面
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入资讯添加页面")
    public String toAdd(Model model) {
    List<SysClassify> listParent = sysClassifyDao.findAllByParentIdAndIsDelete(new BigInteger(Constants.CLASSIFY_INFO),0);
      model.addAttribute("listParent", listParent);
        return "/admin/infoContent/form";
    }

    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入资讯编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(infoContentDao.existsById(new BigInteger(id))) {
                InfoContent infoContent = infoContentDao.getOne(new BigInteger(id));
                model.addAttribute("obj", infoContent);
            }
        }        
        List<SysClassify> listParent = sysClassifyDao.findAllByParentIdAndIsDelete(new BigInteger(Constants.CLASSIFY_INFO),0);
        model.addAttribute("listParent", listParent);
        return "/admin/infoContent/form";
    }

    // 保存
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存资讯信息")
    public JsonResult save(InfoContent infoContent,HttpServletRequest request) {
        JsonResult result = new JsonResult(false,"保存失败");
        String infoIconOld = WebValueUtil.getWebValue(request, "infoIconOld");
        if(!infoIconOld.equals(infoContent.getInfoIcon())) {
        	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
             MultipartFile file = multipartRequest.getFile("file");
             System.out.println(file.toString());
             System.out.println(file.getOriginalFilename());
              // 要上传的目标文件存放路径
             String newFileName = FileUtils.getFileName(file.getOriginalFilename());
        
            // String newFileName = file.getOriginalFilename();
           String imagePath = Constants.getFilePath("info")+newFileName;
             String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");
            // String localPath = Constants.IMAGE_LOCAL_PATH;
             FileUtils.upload(file, localPath, newFileName);
             infoContent.setInfoIcon(imagePath);
        }
        infoContentDao.saveAndFlush(infoContent);
        result.setFlag(true);
        result.setMsg("保存成功");
        return result;
    }


    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除资讯信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (infoContentDao.existsById(id)) {
            InfoContent infoContent = infoContentDao.getOne(id);
            infoContent.setIsDelete(Constants.DELETE);
            infoContentDao.saveAndFlush(infoContent);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }


    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复资讯信息")
    public JsonResult recovery(BigInteger id) {
        JsonResult result = new JsonResult(false,"恢复失败");
        if (infoContentDao.existsById(id)) {
            InfoContent infoContent = infoContentDao.getOne(id);
            infoContent.setIsDelete(Constants.RECOVERY);
            infoContentDao.saveAndFlush(infoContent);
            result.setFlag(true);
            result.setMsg("恢复成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }

}
